package com.cskaoyan.controller.mallController;

import com.cskaoyan.exception.ValueException;
import com.cskaoyan.model.bean.Brand;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.bo.mall.BrandCreatBo;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.mall.BrandVo;
import com.cskaoyan.service.mall.MallService;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Digits;
import java.math.BigDecimal;

@RestController
@RequestMapping("/admin/brand")
public class MallBrandController {


    private ObjectMapper objectMapper = new ObjectMapper();

    {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }


    @Autowired
    MallService mallService;

    @RequiresPermissions("admin:brand:list")
    @RequestMapping("/list")
    // /admin/brand/list?page=1&limit=20&sort=add_time&order=desc
    public BaseRespVo getBrandList(PageBo pageBo, String name, String id) {
        Integer i = null;
        if (id != null && !"".equals(id)){
            try {
                i = Integer.parseInt(id);
            } catch (NumberFormatException e) {
                return BaseRespVo.error(402, "id参数有问题");
            }
        }
        BrandVo brandList = mallService.getBrandList(pageBo, i, name);

        return BaseRespVo.ok(brandList);
    }

    @RequiresPermissions("admin:brand:create")
    @RequestMapping("/create")
    public BaseRespVo brandCreate(@RequestBody BrandCreatBo brandCreatBo) throws ValueException {
        BigDecimal bd;
        try{
            bd = new BigDecimal(brandCreatBo.getFloorPrice());
            bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
            brandCreatBo.setFloorPrice(bd.toString());
        }catch(NumberFormatException  e)
        {
            return BaseRespVo.error(402,"参数类型要为正数");
        }

        if (bd.compareTo(BigDecimal.ZERO) < 0) {
            throw new ValueException(brandCreatBo.getFloorPrice(),"为负数,价格不能为负数");
        }else if (bd.compareTo(BigDecimal.ZERO) == 0){
            brandCreatBo.setFloorPrice("0");
        }
        Brand brand = mallService.brandCreate(brandCreatBo);
        return BaseRespVo.ok(brand);

    }

    @RequiresPermissions("admin:brand:update")
    @RequestMapping("/update")
    public BaseRespVo brandUpdate(@RequestBody Brand brand) throws ValueException {
        BigDecimal bd;
        try{

            bd = new BigDecimal(brand.getFloorPrice());
            bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
            brand.setFloorPrice(bd.toString());
        }catch(NumberFormatException e)
        {
            return BaseRespVo.error(402,"参数类型要为正数,不要家乱七八糟的东西，别写一堆 负号，前端有bug");
        }
        if (bd.compareTo(BigDecimal.ZERO) < 0) {
            throw new ValueException(brand.getFloorPrice(), "为负数,价格不能为负数,不要当舔狗!!!");
        } else if (bd.compareTo(BigDecimal.ZERO) == 0){
            brand.setFloorPrice("0");
        }
        Brand brand1 = mallService.brandUpdate(brand);
        if (brand1 == null) {
            return BaseRespVo.error(402,"更改失败");
        }
        return BaseRespVo.ok(brand1);

    }

    @RequiresPermissions("admin:brand:delete")
    @RequestMapping("/delete")
    public BaseRespVo brandDelete(@RequestBody Brand brand){
        int i = mallService.brandDelete(brand.getId());
        if(i ==1)
        return BaseRespVo.ok();
        else
            return BaseRespVo.error(402,"删除失败");
    }

}
